<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;


class PicController extends Controller
{


    public function getAdd(){
        
        return view('admin/pic/add');
    }

    //图片添加
    public function postInsert(Request $request){
    //dd($request -> all());
    //数据处理
    $arr = $request->only(['name','pic','status','url']);
    $arr['createtime'] = date('Y-m-d H:i:s',time()); 
    // dd($arr);
    if($request -> hasFile('pic')){
            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('pic') -> getClientOriginalExtension();
            // 拼接文件名
            $tname = $temp_name.'.'.$hz;
            $request -> file('pic') -> move('./upload/pic',$tname);
        }

    $arr['pic'] = $tname; 
    // dd($arr); 
    //进行添加
   $res = DB::table('pic') -> insert($arr);
      
    if($res){
            return redirect('/admin/pic/index') -> with('success','添加成功');
       }else{
            return back() -> with('error','添加失败');
       }     
    
    }

        //查询
    public function getIndex(Request $request){
        if(empty(session('id'))){
            return redirect('/admin')->with('error','请您登陆');
        }
        //echo 123123;
        // 分页
        $count = $request -> input('count',10);
       // 关键字
        $search = $request -> input('search','');
        // 对查询语句构造器进行分页  paginate  查询数据并且分页
        $data = DB::table('pic') ->where('name','like','%'.$search.'%')-> paginate($count);
        return view('admin/pic/index ',['data'=>$data,'count'=>$count,'search'=>$search,'request'=>$request->all()]);
    }


    //删除
    public function getDelete($id){
        //echo 11111;
        // dd($id);
        $data = DB::table('pic') -> where('id',$id) -> delete();

        if($data = 1){
         return redirect('/admin/pic/index') -> with('success','删除成功');
      }else{
        return back() -> with('error','删除失败');
      }
    }

    //修改

    public function getEdit($id){
         //查询单条数据
      $data = DB::table('pic') -> where('id',$id) -> first();
      return view('admin/pic/edit',['data'=>$data]);
     }
    //修改
     
    public function postUpdate(Request $request){

        $name = $request->input('name');
        $status = $request->input('status');
        $id  = $request->input('id');
        $url = $request->input('url'); 
        //判断是否修改头像
        if($request -> hasFile('pic')){
            //随机文件名
            $tem_name = md5(time().rand(10000,99999));
            //获取上传文件的后缀名
            $hou_name = $request->file('pic')->getClientOriginalExtension();
            //拼接
            $pic = $tem_name.'.'.$hou_name;
            //移动到指定的文件夹
             $request -> file('pic') -> move('./upload/pic',$pic);
            //dd($pic);
        }else{
            $pic = $request->input('oldpic');
            //dd($pic);
        }
        //将值更新到数据库中
       $data = DB::table('pic')->where('id',$id)->update(['name'=>$name,'pic'=>$pic,'status'=>$status,'url'=>$url]); 

       if($data = 1){
        return redirect('/admin/pic/index') -> with('success','修改成功');
        }else{
       return back() -> with('error','修改失败');
        } 

     }


}
